package test;

import org.apache.commons.httpclient.HttpConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.Scanner;
import download.DownloadPage;


/**
 * Created by Zhangyu on 2014/7/30.
 */
public class MeituanCrawl {
    static String url;

    static  String getcontentFromUrl(String url) {
        String list = "";
        int page;
        try {
            URL path = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) path.openConnection();

            connection.setConnectTimeout(30000);
            connection.setReadTimeout(30000);
            connection.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11");
            connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
//          connection.setRequestProperty("Referer", "www.meituan.com");
            connection.setRequestProperty("Cache-Control", "max-age=0");
            connection.setRequestProperty("Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");


            connection.connect();
            InputStream urlStream = connection.getInputStream();
            BufferedReader br;
            br = new BufferedReader(new InputStreamReader(urlStream, "UTF-8"));
            char[] buf = new char[2048];
            int n = 0;
            while ((n = br.read(buf, 0, 2048)) != -1) {
                list += new String(buf, 0, n);
            }
            br.close();
            urlStream.close();
/**
 int index = list.indexOf("</a></li><li class=\"next\">") - 2;
 String pg = list.substring(index, list.indexOf("</a></li><li class=\"next\">"));
 if (pg.contains(">"))
 pg = pg.substring(pg.indexOf(">") + 1);
 page = Integer.parseInt(pg);
 System.out.println("一共有" + page + "页");
 **/
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return list;
    }

    //http://cd.meituan.com/deal/871990.html
    static void classifyItem(String itemUrl){

        String content = getcontentFromUrl(itemUrl);
        System.out.println("店铺url："+itemUrl);
//      System.out.println(itemUrl);
//      System.out.println(content);
        Document doc = Jsoup.parse(content);


        //获取店铺地区
        try{
            String area = doc.select("span.deal-component-title-prefix").text();
            System.out.println("店铺地区："+area);
        }catch (Exception e){
            System.err.println("获取店铺地区失败！");
        }

        //获取店铺名称
        try{
            String name = doc.getElementsByClass("deal-component-title").text();
            System.out.println("店铺名称："+name);
        }catch (Exception e){
            System.err.println("获取店铺名称失败！");
        }

        //价格
        try{
            Elements e = doc.getElementsByClass("deal-menu-summary");
            String s = e.get(e.size()-1).text();
            String price1 = s.split("￥")[s.split("￥").length-1];
            System.out.println("价格："+price1+"元");
        }catch(Exception e){
            System.err.println("获取价格失败！");
        }

        //已售
        try{
            int start = content.indexOf("<span class=\"deal-component-rating-sold-count hidden orange\"><strong>");
            int end = content.indexOf("</strong>", start);
            String sales = content.substring(start+69,end);
            System.out.println("已售"+sales+"份");
        }catch (Exception e){
            System.err.println("获取已售份数失败！");
        }

        //评分
        try{
            int start = content.indexOf("<span class=\"deal-component-rating-stars orange hidden\">");
            int end = content.indexOf("</span>分",start);
            String rates = content.substring(start+56,end);
            if(rates.length() > 10) {
                System.out.println("评分：暂无");
                rates = null;
            }
            else
                System.out.println("评分："+rates);
        }catch (Exception e){
            System.err.println("获取评分失败！");
        }

        //信息
        try{
            String message = doc.getElementsByClass("deal-component-description").text();
            System.out.println("信息："+message);
        }catch (Exception e){
            System.err.println("获取信息失败！");
        }

        //图片url
        try{
            String imgurl = doc.select("img.focus-view").attr("src");
            System.out.println("图片url："+imgurl);
        }catch (Exception e){
            System.err.println("获取图片url失败！");
        }

    }

    public static void main(String args[]) {


//      System.out.print(list);

        System.out.println("输入你要搜索的关键字：");
        Scanner scanner = new Scanner(System.in);
        String keyword = scanner.nextLine();
        String url = "http://cd.meituan.com/s/"+keyword;
        String list = getcontentFromUrl(url);
        int page;
        int index = list.indexOf("</a></li><li class=\"next\">") - 2;
        String pg = list.substring(index, list.indexOf("</a></li><li class=\"next\">"));
        if (pg.contains(">"))
            pg = pg.substring(pg.indexOf(">") + 1);
        page = Integer.parseInt(pg);
        int amounts = 0;

/**
        for (int j = 1; j <= page; j++) {
            String keyword1 = keyword+"/page"+j;
            list = getlistFromUrl(keyword1);
            if (!list.equals("")) {
                Document doc = Jsoup.parse(list);

                Elements titles = doc.getElementsByClass("deal-tile__title");
                Elements details = doc.getElementsByClass("deal-tile__detail");

                for (int i = 0; i < (titles.size() < details.size() ? titles.size() : details.size()); i++) {
                    String title = titles.get(i).select("a[title]").text();
                    Double price;
                    try {
                        price = Double.parseDouble(details.get(i).select("strong").text());
                    }catch(Exception e){
                        price = 0.0;
                    }
                    System.out.println(title + "\t" + price + "元");
                    amounts++;
                }

            } else
                System.err.println("爬取失败");
        }
**/

        for(int j = 1;j <= page;j++){
            String url1 = "http://cd.meituan.com/s/"+keyword+"/page"+j;
            String content = getcontentFromUrl(url1);
            //为json对象，可以解析json获取deals数组
            int start = content.indexOf("\"deals\":\"");
            int end = content.indexOf("\"},\"gaCustomVar\"",start);
            String itemIds[] = content.substring(start+9,end).split(",");
            for(int i = 0;i < itemIds.length;i++) {
//              System.out.println(itemIds[i]);
                amounts++;
            }
            amounts--;
            for(int i = 0;i <= amounts;i++){
                String itemurl = "http://cd.meituan.com/deal/"+itemIds[i]
                        +".html";
                classifyItem(itemurl);
            }

        }
        System.out.println("共爬取"+amounts+"条数据");
    }




}


//新品：http://cd.meituan.com/s/keyword/new
//按销量：http://cd.meituan.com/s/keyword/hot
//按价格：http://cd.meituan.com/s/keyword/price-asc
//按评分：http://cd.meituan.com/s/keyword/rating
//按时间：http://cd.meituan.com/s/keyword/time